package com.example.educate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.educate.mapper.ChapterMapper;
import com.example.educate.model.vod.Chapter;
import com.example.educate.model.vod.Video;
import com.example.educate.model.vod.VideoVisitor;
import com.example.educate.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.educate.service.VideoService;
import com.example.educate.vo.vod.ChapterVo;
import com.example.educate.vo.vod.VideoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author AT
 * @since 2022-09-18
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {
    @Autowired
    private VideoService videoService;

    @Override
    public List<ChapterVo> getTreeList(Long courseId) {
        ArrayList<ChapterVo> chapterVos = new ArrayList<>();
        //根据课程id获取所有的章节
        QueryWrapper<Chapter> chapterWrapper = new QueryWrapper<>();
        QueryWrapper<Chapter> ChapterList = chapterWrapper.eq("course_id", courseId);
        List<Chapter> chapters = baseMapper.selectList(chapterWrapper);
        //根据课程id获取所有的小节
        QueryWrapper<Video> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id", courseId);
        List<Video> videoList = videoService.list(videoWrapper);
        for (int i = 0; i < chapters.size(); i++) {
            Chapter chapter=chapters.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            chapterVos.add(chapterVo);
            ArrayList<VideoVo> videoVos = new ArrayList<>();
            for (int i1 = 0; i1 < videoList.size(); i1++) {
                Video video = videoList.get(i1);
                if(chapter.getId().equals(video.getChapterId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video,videoVo);
                    videoVos.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVos);
        }
        return chapterVos;
    }
}
